/*
	This file is part of Floculate.
	Copyright (C) 2008  Bill Whitacre

	Floculate is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Floculate is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "projection.h"

void rat_build_frustum(rat_projection *proj,rat_frustum *frustum)
{
	memcpy(&(frustum->znear),&(proj->znear),sizeof(float)*2);
	memcpy(&(frustum->vpx),&(proj->vpx),sizeof(int)*4);
	
	frustum->top=frustum->znear*tanf(180.0f/(MATH_PI*proj->yfov));
	frustum->bottom=-frustum->top;
	frustum->left=frustum->bottom*proj->aspect;
	frustum->right=frustum->top*proj->aspect;
}

void rat_projection_push_frustum(rat_frustum *frustum)
{
	glMatrixMode(GL_PROJECTION);
	glViewport(frustum->vpx,frustum->vpy,frustum->vpw,frustum->vph);
	glPushMatrix();
	glLoadIdentity();
	glFrustum(frustum->left,frustum->right,
		frustum->bottom,frustum->top,frustum->znear,frustum->zfar);
		
	glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_ENABLE_BIT);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);

}

void rat_projection_pop()
{
	glPopAttrib();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}

